/**
 * Created by shandianhaizan on 16/7/13.
 */
define(['validator'], function (validator) {

    function _controller($scope, $routeParams, $location, $timeout, httpService) {

        var id = $routeParams.id;
        var model ="Coupon";


        var isUpdate = function(){
            return id && id!=="new"
        };


        var inArray = function(one, arr){
            return $.inArray(one, arr)>=0
        };
        var inProperty = function(one , arr){
            for(var i= 0, l=arr.length; i<l; i++){
                if(one.name == arr[i].key){
                    return true;
                }
            }
            return false;
        };
        var inRelation = function(one , arr){
            for(var i= 0, l=arr.length; i<l; i++){
                if(one.objectId == arr[i].objectId){
                    return true;
                }
            }
            return false;
        };



        $scope.remove = function(one, arr){
            arr.splice( $.inArray(one, arr), 1);
        };


        $scope.validation = function(arg_params){
            if(!arg_params.name) return {success:false, msg:"优惠券名称未填"};
            if(arg_params.priceWay == 'set' && !arg_params.value) return {success:false, msg:"优惠面值未填完整"};
            if(arg_params.priceWay == 'random' && ( !arg_params.randomLte ||  arg_params.randomGte > arg_params.randomLte )){
                return {success:false, msg:"优惠面值未填完整"};
            }
            if(!arg_params.startTime || !arg_params.endTime || arg_params.startTime >= arg_params.endTime)   return {success:false, msg:"开始时间必须小于结束时间"};
            return {success:true}
        };


        $scope.save = function () {
            $scope.$broadcast('show-errors-check-validity');
            if (!$scope.userForm.$valid) return;
            var validation = $scope.validation($scope.coupon);
            if(!validation.success) return $scope.msg.show(validation.msg);
            httpService.save(model, $scope.coupon).then(function (data) {
                $scope.msg.show(data);
                if($scope.isHandleSuccess(data) && !isUpdate()){
                    $location.path('/edit-coupon/'+data.data.objectId);
                }
            });

        };


        $scope.searchProduction = function (keyword) {
            var query = {};
            var type = $scope.searchProType || "name";
            query[type] = keyword;
            return httpService.get("Production",query);
        };









        $scope.reset = function () {
            $scope.$broadcast('show-errors-reset');
            $scope.order = {};
        };






        //添加产品
        $scope.addProduction= function(c){
            if(!c || !c.objectId) return;
            $scope.coupon.production =   $scope.coupon.production || [];
            if(!inRelation(c, $scope.coupon.production)) {
                $scope.coupon.production.push({objectId: c.objectId,name: c.name});
            }
        };

        $scope.initGroup = function () {
            httpService.get("Group", {all:true}).then(function (data) {
                $scope.groups = data.data.data;
            });
        };

        $scope.initGroup();


        if(isUpdate()){
            $scope.editType = "编辑";
            httpService.get(model, id).then(function (data) {
                $scope.coupon = data.data;
            });
        }else{
            $scope.editType = "添加";
            $scope.coupon = {suit:'all', priceWay:'set'};
        }



    }

    return _controller;
});

